// 1.引入要是用到API
import {defineStore} from 'pinia'

// 2.引入接口函数
import {reqGetCategory1ListData,reqGetCategory2ListData,reqGetCategory3ListData} from '@/api/product/category'
import type { CategoryStateModel } from '@/api/product/model/categoryModel'
// 3.定义对象并导出
export const useCategoryListStore = defineStore('categoryList',{
  state:():CategoryStateModel=>{
    return{
      // 收集用户真正选择的分类的id
      category1Id:undefined,
      category2Id:undefined,
      category3Id:undefined,

      // 收集服务端响应回来的数据
      category1List:[],
      category2List:[],
      category3List:[]
    }
  },
  actions:{
    // 1.获取一级导航数据
    async getCategory1ListData(){
      const result = await reqGetCategory1ListData()
      // console.log('rr',result);
      // console.log(this); 
      this.category1List = result
    },

    // 2.获取二级导航数据
    async getCategory2ListData(id:number) {
      const result = await reqGetCategory2ListData(id)
      this.category2List = result
      this.category1Id = id  //获取id是为了查询二级导航的数据
      // 如果一级导航改变二级三级需要清空重置
      this.category2Id = undefined
      this.category3Id = undefined

    },

    // 3.获取三级导航数据
    async getCategoty3ListData(id:number) {
      const result = await reqGetCategory3ListData(id)
      this.category2Id = id
      this.category3Id = undefined
      this.category3List = result
    },

    //设置三级分类id
    setCategory3Id(id:number) {
      this.category3Id = id
    }
  },
  getters:{

  }

})